<?php


namespace app\hapi\controller;


use app\common\controller\Tree;
use app\common\model\AuthGroup;
use app\common\model\AuthRule;
use think\Controller;

class Auth extends ApiBase
{
    public function getroutes()
    {
        $re_param['return_code'] = "SUCCESS";
        $re_param['return_msg'] = "成功";
        $AuthRule=new AuthRule();
        $Tree=new Tree();
        $list=$AuthRule->order(['id'=>'asc'])->column('*','id');
        foreach($list as $k=>$v){
            $list[$k]['meta']=[
                'title'=>$v['title'],
                'noCache'=>$v['noCache'],
            ];
            unset($v['title']);
            unset($v['noCache']);
        }

        $data=$Tree->listToTreeMulti($list, 0, 'id', 'pid', 'children');
        $re_param['data'] = $data;
        return $re_param;
    }
    public function getauthgroup($param)
    {
        $map = [];
        $AuthGroup = new AuthGroup();
        $list = $AuthGroup->where($map)->order('id asc')->select();
        foreach($list as $k=>$v){
            $list[$k]['updateTime']=date('Y-m-d H:i:s',$v['updateTime']);
        }
        $re_data['return_code'] = "SUCCESS";
        $re_data['return_msg'] = "成功";
        $re_data['data']['items'] = $list;
        return $re_data;
    }
    public function authgroupadd($param){
        $rules=implode(',',$param['authmenu']);
        $data['title']=$param['title'];
        $data['description']=$param['description'];
        $data['rules']=$rules;
        $data['status']=1;
        $data['updateTime']=time();
        $add=AuthGroup::create($data);
        if($add){
            $re_data['return_code'] = "SUCCESS";
            $re_data['return_msg'] = "成功";
            $re_data['data']['id'] = $add;
        }else{
            $re_data['return_code'] = "FAIL";
            $re_data['return_msg'] = "添加失败";
        }
        return $re_data;
    }
    public function authgroupedit($param){
        $rules=implode(',',$param['authmenu']);
        $data['id']=$param['id'];
        $data['title']=$param['title'];
        $data['description']=$param['description'];
        $data['rules']=$rules;
        $data['status']=1;
        $data['updateTime']=time();
        $add=AuthGroup::update($data);
        if($add){
            $re_data['return_code'] = "SUCCESS";
            $re_data['return_msg'] = "修改成功";
            $re_data['data']['id'] = $add;
        }else{
            $re_data['return_code'] = "FAIL";
            $re_data['return_msg'] = "修改失败";
        }
        return $re_data;
    }
    public function authgroupdel($param){
        $del=AuthGroup::destroy(['id'=>$param['id']]);
        if ($del) {
            $re_data['return_code'] = "SUCCESS";
            $re_data['return_msg'] = "成功";
        } else {
            $re_data['return_code'] = "FAIL";
            $re_data['return_msg'] = "删除失败";
        }
        return $re_data;
    }
    public function getmenuinfo($param){
        $info=AuthRule::get(['id'=>$param['id']]);
        $list=AuthRule::all();
        $Tree=new Tree();
        $pids=$Tree->getlistpid(collection($list)->toArray(),$param['id']);
        $info['pids']=array_diff(array_column($pids,'id'),[$param['id']]);;
        $re_data['return_code'] = "SUCCESS";
        $re_data['return_msg'] = "成功";
        $re_data['data']['info'] = $info;
        return $re_data;

    }
    public function menuadd($param){

        $datas=json_decode($param['data'], true);
        $data['title']=$datas['title'];
        $data['pid']=$datas['pid'];
        $data['name']=$datas['name'];
        $data['status']=$datas['status'];
        $data['icon']=$datas['icon'];
        $data['path']=$datas['path'];
        $data['component']=$datas['component'];
        $data['hidden']=$datas['hidden'];
        $data['noCache']=$datas['noCache'];
        $data['alwaysShow']=$datas['alwaysShow'];
        $data['redirect']=$datas['redirect'];
        $add=AuthRule::create($data);
        if($add){
            $re_data['return_code'] = "SUCCESS";
            $re_data['return_msg'] = "成功";
            $re_data['data']['id'] = $add;
        }else{
            $re_data['return_code'] = "FAIL";
            $re_data['return_msg'] = "添加失败";
        }
        return $re_data;

    }
    public function menuedit($param){

        $datas=json_decode($param['data'], true);
        $data['title']=$datas['title'];
        $data['id']=$datas['id'];
        $data['pid']=$datas['pid'];
        $data['name']=$datas['name'];
        $data['status']=$datas['status'];
        $data['icon']=$datas['icon'];
        $data['path']=$datas['path'];
        $data['component']=$datas['component'];
        $data['hidden']=$datas['hidden'];
        $data['noCache']=$datas['noCache'];
        $data['alwaysShow']=$datas['alwaysShow'];
        $data['redirect']=$datas['redirect'];
        $add=AuthRule::update($data);
        if($add){
            $re_data['return_code'] = "SUCCESS";
            $re_data['return_msg'] = "修改成功";
            $re_data['data']['id'] = $add;
        }else{
            $re_data['return_code'] = "FAIL";
            $re_data['return_msg'] = "修改失败";
        }
        return $re_data;
    }
    public function menudel($param){
        $del=AuthRule::destroy(['id'=>$param['id']]);
        if ($del) {
            $re_data['return_code'] = "SUCCESS";
            $re_data['return_msg'] = "成功";
        } else {
            $re_data['return_code'] = "FAIL";
            $re_data['return_msg'] = "删除失败";
        }
        return $re_data;
    }

}